package com.nameless.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nameless.entity.CouponHistory;
import com.nameless.service.CouponHistoryService;
import com.nameless.vo.CouponHistoryVo;
import com.nameless.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2023/02/27 21:14
 * @Description
 */
@RestController
@RequestMapping("/coupon/api/couponHistory")
@Api("优惠劵历史api信息操作接口")
@Slf4j
public class ApiCouponHistoryController {
    @Autowired
    private CouponHistoryService couponHistoryService;

    @PostMapping("/queryAllByParam/{pageNo}/{pageSize}")
    @ApiOperation(value = "分页查询信息并携带查询条件")
    public Result<Page<CouponHistory>> findPageCustomer(@PathVariable Integer pageNo,@PathVariable Integer pageSize,
                                                        @RequestBody CouponHistory history ){
        return Result.success(couponHistoryService.pageByNames(pageNo,pageSize,history));
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据优惠劵历史编号信息")
    public Result<CouponHistory> getOne(@PathVariable Long id){
        CouponHistory byId = couponHistoryService.getById(id);
        return Result.success("id为："+id+"的优惠劵查询成功",byId);
    }

    @GetMapping("/client/{clientId}")
    @ApiOperation(value = "根据会员编号查询信息")
    public Result<List<CouponHistory>> getOne( @PathVariable String clientId){
        LambdaQueryWrapper<CouponHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CouponHistory::getClientId,clientId);
        List<CouponHistory> couponHistoryList = couponHistoryService.list(queryWrapper);
        return Result.success("会员id为："+clientId+"的优惠劵查询成功",couponHistoryList);
    }


    @DeleteMapping
    @ApiOperation(value = "删除多个优惠劵历史")
    @ApiImplicitParam(name = "idList",value = "需要删除的医院配置id集合")
    public Result<String> delete(@RequestBody List<Integer> idList){
        log.info("idList = {}",idList);
        boolean b = couponHistoryService.removeByIds(idList);
        return b?Result.success("删除成功"):Result.error("删除失败");
    }

    @PostMapping
    @ApiOperation(value = "添加优惠劵历史信息")
    public Result<String> addCustomer(@RequestBody CouponHistoryVo couponHistoryVo){
        return couponHistoryService.saveBycouponHistoryVo(couponHistoryVo);
    }

    @PutMapping
    @ApiOperation(value = "修改优惠劵历史信息")
    public Result<String> editCustomer(@RequestBody CouponHistory coupon){
        boolean save = couponHistoryService.updateById(coupon);
        return save?Result.success("修改成功"):Result.error("修改失败");
    }
}
